package com.wsx.rpc.metrics.counter;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;


/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.metrics.counter.CounterTest
 * @Description: 唯独一个度量单位的计数器
 * @date 2018/11/26
 */
public class CounterTest {


    private static final MetricRegistry REGISTRY = new MetricRegistry();

    private static ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(REGISTRY).build();

    private static Counter counter = REGISTRY.counter(MetricRegistry.name(CounterTest.class, "queue-size"));


    private static Queue<String> queue = new LinkedBlockingQueue<>();

    private static void add(String str) {
        counter.inc();
        queue.offer(str);
    }

    private static String take() {
        counter.dec();
        return queue.poll();
    }

    public static void main(String[] args) {
        consoleReporter.start(3, TimeUnit.SECONDS);
        boolean go = true;
        int num = 1;
        while (go) {
            if (num >= 10) {
                go = false;
            }
            add(String.valueOf(num));
            num ++;
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
